home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdio / fgets.c < prev    next >
C/C++ Source or Header  |  1992-03-31  |  3KB  |  122 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. /* Reads characters from STREAM into S, until either a newline character
  25.    is read, N - 1 characters have been read, or EOF is seen.  Returns
  26.    the newline, unlike gets.  Finishes by appending a null character and
  27.    returning S.  If EOF is seen before any characters have been written
  28.    to S, the function returns NULL without appending the null character.
  29.    If there is a file error, always return NULL.  */
  30. char *
  31. DEFUN(fgets, (s, n, stream), char *s AND size_t n AND register FILE *stream)
  32. {
  33.   register char *p = s;
  34.  
  35.   if (!__validfp(stream) || s == NULL || n == 0)
  36.     {
  37.       errno = EINVAL;
  38.       return NULL;
  39.     }
  40.  
  41.   if (ferror (stream))
  42.     return NULL;
  43.  
  44.   if (stream->__buffer == NULL && stream->__userbuf)
  45.     {
  46.       /* Unbuffered stream.  Not much optimization to do.  */
  47.       register int c = 0;
  48.       while (--n > 0 && (c = getc (stream)) != EOF) 
  49.     if ((*p++ = c) == '\n')
  50.       break;
  51.       if (c == EOF && (p == s || ferror (stream)))
  52.     return NULL;
  53.       *p = '\0';
  54.       return s;
  55.     }
  56.  
  57.   /* Leave space for the null.  */
  58.   --n;
  59.  
  60.   if (n > 0 &&
  61.       (!stream->__seen || stream->__buffer == NULL || stream->__pushed_back))
  62.     {
  63.       /* Do one with getc to allocate a buffer.  */
  64.       int c = getc (stream);
  65.       if (c == EOF)
  66.     return NULL;
  67.       *p++ = c;
  68.       if (c == '\n')
  69.     {
  70.       *p = '\0';
  71.       return s;
  72.     }
  73.       else
  74.     --n;
  75.     }
  76.  
  77.   while (n > 0)
  78.     {
  79.       size_t i;
  80.       char *found;
  81.  
  82.       i = stream->__get_limit - stream->__bufp;    
  83.       if (i == 0)
  84.     {
  85.       /* Refill the buffer.  */
  86.       int c = __fillbf (stream);
  87.       if (c == EOF)
  88.         break;
  89.       *p++ = c;
  90.       --n;
  91.       if (c == '\n')
  92.         {
  93.           *p = '\0';
  94.           return s;
  95.         }
  96.       i = stream->__get_limit - stream->__bufp;    
  97.     }
  98.  
  99.       if (i > n)
  100.     i = n;
  101.  
  102.       found = (char *) __memccpy ((PTR) p, stream->__bufp, '\n', i);
  103.  
  104.       if (found != NULL)
  105.     {
  106.       stream->__bufp += found - p;
  107.       p = found;
  108.       break;
  109.     }
  110.  
  111.       stream->__bufp += i;
  112.       n -= i;
  113.       p += i;
  114.     }
  115.  
  116.   if (p == s)
  117.     return NULL;
  118.  
  119.   *p = '\0';
  120.   return ferror (stream) ? NULL : s;
  121. }
  122.